package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name ="展示所有角色数据")
    public String findAll(@RequestParam(name = "page",defaultValue = "1")int pageNum, @RequestParam(name = "pageSize",defaultValue = "10")int pageSize){

        PageInfo<Role> pageInfo = roleService.findPage(getCompanyId(),pageNum, pageSize);

        request.setAttribute("page",pageInfo);
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toAdd",name ="进入角色新增页面")
    public String toAdd(){
        return "system/role/role-add";
    }

    @RequestMapping(value = "/edit",name ="保存角色方法")
    public String edit(Role role){
        if (StringUtils.isEmpty(role.getId())){
            //id为空说明是新增
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            role.setCreateTime(new Date());
            role.setId(UUID.randomUUID().toString());
            roleService.save(role);
        }
        else {
            //id不为空则说明为修改
            role.setUpdateTime(new Date());
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }
    @RequestMapping(value = "/toUpdate",name ="进入角色修改页面")
    public String toUpdate(String id){
        Role role=roleService.findById(id);
        request.setAttribute("role",role);
        return "system/role/role-add";
    }
    @RequestMapping(value = "/delete",name ="删除角色信息")
    public String delete(String id){
        roleService.deleteById(id);
        return "redirect:/system/role/list.do";
    }
  @RequestMapping(value = "/roleModule",name ="进入分配菜单权限页面")
    public String roleModule(String roleid) {
      Role role = roleService.findById(roleid);
      request.setAttribute("role", role);
      return "system/role/role-module";

  }
    @RequestMapping(value = "/getZtreeNodes",name ="构造模块数据")
    @ResponseBody
    public List<Map> getZtreeNodes(String roleid){
       //查询此角色所拥有的菜单权限
       List<String> moduleIdList= moduleService.findModulesByRoleId(roleid);
        //查询所有模块数据
        List<Module> moduleList = moduleService.findModuleListByCompanyId(getCompanyId());
        //构造map集合
        List<Map> listMap=new ArrayList<>();
        Map map=null;
        for (Module module : moduleList) {
            map=new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            //判断moduleIdList集合中是否包含module.getId();
            if (moduleIdList.contains(module.getId())){
                map.put("checked",true); //若包含该id  则选中
            }
            listMap.add(map);
        }

        return listMap;
    }
    @RequestMapping(value = "/updateRoleModule",name ="实现权限分配")
    public String updateRoleModule(String roleid,String moduleIds){
      roleService.updateRoleModule(roleid,moduleIds);
      return "redirect:/system/role/list.do";
    }

}
